package com.ewentsai;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@WebServlet(name = "RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String password1 = (String) request.getAttribute("Paswword_1");
        String password2 = (String) request.getAttribute("Paswword_2");
        if(password1!=password2){
            response.sendRedirect("RegisterFail.html");
            return;
        }
        String username = request.getParameter("username");
        String password = request.getParameter("Password_1");
        String nickname = request.getParameter("nickname");
        Connection connection = DB.getConnection();
        Statement statement = DB.createStatement(connection);
        DB.executeUpdate(statement, "insert into users (username,password,nickname) values (\""+username+"\",\""+password+"\",\""+nickname+"\")");
        response.sendRedirect("RegisterSuccess.html");
    }
}
